import xlwt
from utils.common_utils import get_all_fields


def save_objs_to_excel(infos, obj, file_name='1.xls'):
    # 获取obj对象的字段
    fields = get_all_fields(obj)
    # 将查询结果写入到excel
    workbook = xlwt.Workbook()
    # 创建一个新的sheet
    sheet = workbook.add_sheet('sheet1', cell_overwrite_ok=True)
    # 将表的字段名写入excel
    for i in range(len(fields)):
        sheet.write(0, i, fields[i])
    # 结果写入excel
    for row in range(1, len(infos) + 1):
        for col in range(len(fields)):
            sheet.write(row, col, infos[row - 1][col])
    # excel保存为文件
    workbook.save('files/outs/' + file_name)


if __name__ == '__main__':
    pass
